<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<ul class="breadcrumb panel">
    <li><a href="javascript:backMainFun();"><i class="fa fa-home"></i> 首页</a></li>
    <li><a href="javascript:loadFun('/role/toRoleIndex');">角色管理</a></li>
    <li class="active">修改角色</li>
</ul>

<div class="panel">
    <header class="panel-heading">
        修改角色
    </header>
    <div class="panel-body">
        <form action="${ctx}/role/doSave" name="roleForm" id="roleForm" method="post" class="form-horizontal">
            <input type="hidden" id="role_id" name="role_id" value="${role.role_id}">
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label must">角色名</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="name" name="name" value="${role.name}"
                           placeholder="请输入角色名">
                </div>
            </div>
            <div class="form-group">
                <label for="remark" class="col-sm-2 control-label">备注</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="remark" name="remark" value="${role.remark}"
                           placeholder="···">
                </div>
            </div>
            <div class="row">
                <div class="col-xs-10">
                    <button type="button" class="btn btn-primary pull-right" id="subBtn"><i
                            class="glyphicon glyphicon-saved"></i> 保存
                    </button>
                    <button type="button" class="btn btn-default pull-right marR" onclick="backFun()"><i
                            class="glyphicon glyphicon-chevron-left"></i> 返回
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>

<script>
    function backFun() {
        loadFun("/role/toRoleIndex");
    }

    $('#name').blur(function () {
        if ($('#name').val() != false) {
            $.post("${ctx}/role/checkName",
                    {
                        name: $('#name').val()
                    }, function (data) {
                        console.log(data)
                        if (!data.valid) {
                            zAlert("角色名已被占用，请重新填写!");
                            $('#name').focus();
                            $('#subBtn').attr("disabled",true);
                        }else{
                            $('#subBtn').attr("disabled",false);
                        }
                    });
        }

    });


    $('#subBtn').click(function () {
        if ($('#name').val() == false) {
            zAlert('角色名不能为空')
            return false;
        } else {
            $.ajax({
                url: "${ctx}/role/doSave",
                type: 'post',
                data: {
                    role_id: $('#role_id').val(),
                    name: $('#name').val(),
                    remark: $('#remark').val()
                },
                success: function (data) {
                    if (data.success) {
                        zAlert(data.msg);
                        setTimeout(function () {
                            loadFun("/role/toRoleIndex");
                        }, 1000);
                    } else {
                        zAlert(data.msg);
                    }
                }
            })
        }
    })
</script>

